package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.DataCleanManager;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.httpUtils.HttpHeadParams;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyToast;

import org.w3c.dom.Text;


public class SettingActivity extends BaseActivity implements View.OnClickListener ,ContributorView {
    private static final String FLAG = "logout";
    private ImageView regist_back;
    private TextView tv_cache;
    private RelativeLayout bottom_content_two, bottom_content_six,
            bottom_content_eight, bottom_content_five, bottom_content_seven;
    private Context context;
    private MyToast myToast;
    private TextView tv_headtitle;
    private ContributorPresenter contributorPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_layout);
        context=this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
        iniView();
        iniData();
        initEvent();
    }

    private void iniView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        bottom_content_two = (RelativeLayout) findViewById(R.id.bottom_content_two);
        bottom_content_six = (RelativeLayout) findViewById(R.id.bottom_content_six);
        bottom_content_seven = (RelativeLayout) findViewById(R.id.bottom_content_seven);
        bottom_content_eight = (RelativeLayout) findViewById(R.id.bottom_content_eight);
        bottom_content_five = (RelativeLayout) findViewById(R.id.bottom_content_five);
        tv_cache = (TextView) findViewById(R.id.tv_cache);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("设置");
    }

    private void iniData() {
        try {
            String getFormatSize = DataCleanManager.getTotalCacheSize(getApplication());
            tv_cache.setText(getFormatSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        regist_back.setOnClickListener(this);
        bottom_content_two.setOnClickListener(this);
        bottom_content_six.setOnClickListener(this);
        bottom_content_eight.setOnClickListener(this);
        bottom_content_five.setOnClickListener(this);
        bottom_content_seven.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.bottom_content_two:
                Intent changPwd_intent = new Intent(SettingActivity.this, ChangPwdActivity.class);
                startActivity(changPwd_intent);
                break;
            case R.id.bottom_content_six:
                Intent back_advice = new Intent(SettingActivity.this, BackAdiceAvtivity.class);
                startActivity(back_advice);
                break;
            case R.id.bottom_content_eight:
                String  userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                contributorPresenter.logout(userid,FLAG);
                break;
            case R.id.bottom_content_five:
                startActivity(new Intent(SettingActivity.this, AboutMineActivity.class));
                break;
            case R.id.bottom_content_seven:
                DataCleanManager.clearAllCache(getApplication());
                tv_cache.setText("0B");
                Toast.makeText(this, "数据已清空", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {
        super.onLoadContributorStart(flag);
    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        if(FLAG.equals(flag)){
            Tools.setSharedPreferencesboolen(Constant.isLoginIn,false,context);
            Tools.setSharedPreferencesStrings(Constant.SharePreferencePhone,"",context);
            Tools.setSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
            Tools.setSharedPreferencesStrings(Constant.SharePreferenceToken,"",context);
            HttpHeadParams.getInstance().setMobile("");
            Tools.setSharedPreferencesStrings(Constant.SharePreferenceMyModuleInfo,"",context);
            myToast.showShort("已退出登录");
            Intent main_intent = new Intent(context,HomeActivity.class);
            startActivity(main_intent);
            finish();
        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        super.onLoadContributorComplete(flag);
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
        showToast("网络连接异常，请稍后再试");
    }
}
